package com.restaurant.application.root.commonui.data;

import com.google.common.collect.Lists;
import com.nebutown.generation.Convert;
import com.nebutown.generation.PickableBean;

import java.util.List;

public class ShowValue implements PickableBean {

    public static final String FN_NAME = "name";
    public static final String FN_VALUE = "value";
    public static final String FN_LIST = "list";

    public static final List<String> FIELD_NAMES = Lists.newArrayList(
            FN_NAME,
            FN_VALUE,
            FN_LIST);

    private String name;
    private String value;
    private EditorListValue list;

    public ShowValue(String name) {
        setName(name);
    }

    public ShowValue() {
    }

    public ShowValue duplicate() {
        ShowValue result = new ShowValue();
        result.name = this.name;
        result.value = this.value;
        result.list = this.list == null ? null : this.list.duplicate();
        return result;
    }

    @Override
    public String toString() {
        return Convert.toJson(this);
    }

    private void cleanValue() {
        value = null;
        list = null;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        cleanValue();
        this.value = value;
    }

    public ShowValue withValue(String value) {
        setValue(value);
        return this;
    }

    public ShowValue withValue(String value, String defaultValue) {
        setValue(value == null ? defaultValue : value);
        return this;
    }

    public EditorListValue getList() {
        return list;
    }

    public void setList(EditorListValue value) {
        cleanValue();
        this.list = value;
    }

    public ShowValue withList(EditorListValue value) {
        setList(value);
        return this;
    }

    public ShowValue withList(EditorListValue value, EditorListValue defaultValue) {
        setList(value == null ? defaultValue : value);
        return this;
    }

    public String getName() {
        return name;
    }

    public void setName(String value) {
        this.name = value;
    }

    public ShowValue withName(String value) {
        setName(value);
        return this;
    }

    public ShowValue withName(String value, String defaultValue) {
        setName(value == null ? defaultValue : value);
        return this;
    }

    public Object getValueOf(String name) {
        switch (name) {
            case "name":
                return getName();
            case "value":
                return getValue();
            case "list":
                return getList();
        }
        return null;
    }

    public boolean hasField(String name) {
        return FIELD_NAMES.contains(name);
    }

    @Override
    public int hashCode() {
        return signature().hashCode();
    }

    @Override
    public boolean equals(Object o) {
        if (!(o instanceof ShowValue)) return false;
        return signature().equals(((ShowValue) o).signature());
    }

    public String signature() {
        throw new RuntimeException("Bean ShowValue has no signature defined, no hash code or equals possible.");
    }
}
